/*
 *@Type SocketServerHandler.java
 * @Desc
 * @Author urmsone urmsone@163.com
 * @date 2024/6/13 12:50
 * @version
 */
package controller;

import dto.ActionDTO;
import dto.ActionTypeEnum;
import dto.RespDTO;
import dto.RespStatusTypeEnum;
import service.Store;
import utils.LoggerUtil;

import java.io.*;
import java.net.Socket;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class SocketServerHandler implements Runnable {
    private final Logger LOGGER = LoggerFactory.getLogger(SocketServerHandler.class);
    private Socket socket;
    private Store store;

    public SocketServerHandler(Socket socket, Store store) {
        this.socket = socket;
        this.store = store;
    }
@Override
public void run() {
    try (ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
         ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream())) {

        // 接收序列化对象
        ActionDTO dto = (ActionDTO) ois.readObject();
        LoggerUtil.debug(LOGGER, "[SocketServerHandler][ActionDTO]: {}", dto.toString());
        System.out.println("" + dto.toString());

        // 创建命令处理工厂
        CommandHandlerFactory factory = new CommandHandlerFactory();
        CommandHandler handler = factory.createCommandHandler(dto);
        if (handler != null) {
            handler.handleCommand(dto, this.store, oos);
        } else {
            LoggerUtil.error(LOGGER, null, "Unsupported action type: {}", dto.getType());
            RespDTO resp = new RespDTO(RespStatusTypeEnum.FAIL, null);
            oos.writeObject(resp);
            oos.flush();
        }
    } catch (IOException | ClassNotFoundException e) {
        e.printStackTrace();
    } finally {
        try {
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
}
